<?php
 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\AddressModel;
use think\Db;

class AddressController extends AdminBaseController {

    /**
     * 地址管理
     * @adminMenu(
     *     'name'   => '地址',
     *     'parent' => 'admin/address/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 50,
     *     'icon'   => '',
     *     'remark' => '地址管理',
     *     'param'  => ''
     * )
     */
    public function index() {
        $type = $this->request->param('type',1);
        session('addr_type',$type);
        $addressModel = new AddressModel();
        $address = $addressModel->where(['type'=>$type])->select();
        $this->assign('res', $address);

        $this->assign("arrStatus", $addressModel::$STATUS);
        $this->assign('type', session('addr_type'));

        return $this->fetch();
    }

    /**
     * 添加地址
     * @adminMenu(
     *     'name'   => '添加地址',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加地址',
     *     'param'  => ''
     * )
     */
    public function add() {
        $this->assign('type', session('addr_type'));
        $this->assign("arrStatus", AddressModel::$STATUS);
        return $this->fetch();
    }

    /**
     * 添加地址提交保存
     * @adminMenu(
     *     'name'   => '添加地址提交保存',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加地址提交保存',
     *     'param'  => ''
     * )
     */
    public function addPost() {
        $data = $this->request->param();
        if (isset($data['country']) and !empty($data['country'])) {
            $data['coun_id'] = cmf_get_region_id($data['country'],1);
        } else {
            $this->error('请输入国家');
        }
        if (isset($data['province']) and !empty($data['province'])) {
            $data['pro_id'] = cmf_get_region_id($data['province'],2,$data['coun_id']);
        } else {
            $this->error('请输入省/州');
        }
        if (isset($data['city']) and !empty($data['city'])) {
            $data['city_id'] = cmf_get_region_id($data['city'],3,$data['pro_id']);
        } else {
            $this->error('请输入市/区');
        }
        if (!isset($data['zip']) or empty($data['zip'])) {
            $this->error('请输入邮编');
        }
        if ( is_usa($data['country']) ) {
            $data['name'] = $data['country'].' '.$data['detail'].', '.$data['city'].', '.$data['province'].' '.$data['zip'];
        } else {
            $data['name'] = $data['country'].' '.$data['province'].' '.$data['city'].' '.$data['detail'].'，'.$data['zip'];
        }
        unset($data['country']);
        unset($data['province']);
        unset($data['city']);
        
        if (isset($data['code']) and !empty($data['code'])) {
            $length = mb_strlen((string) $data['code']);
            if ($data['type'] == 1) {
                if ($length!=2) {
                    $this->error('驿站编号需输入2位字母');
                }
            } else {
                if ($length!=3) {
                    $this->error('小站编号需输入3位字母');
                }
            }
        }

        $addressModel = new AddressModel();
        $result    = $addressModel->validate(true)->allowField(true)->save($data);
        if ($result === false) {
            $this->error($addressModel->getError());
        }

        $this->success("添加成功！", url("address/index",['type'=>$data['type']]));
    }

    /**
     * 编辑地址
     * @adminMenu(
     *     'name'   => '编辑地址',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑地址',
     *     'param'  => ''
     * )
     */
    public function edit() {
        $id = $this->request->param('id', 0, 'intval');
        $addr = AddressModel::get($id);
        $addr['country'] = cmf_get_region_name($addr['coun_id']);
        $addr['province'] = cmf_get_region_name($addr['pro_id']);
        $addr['city'] = cmf_get_region_name($addr['city_id']);
        $this->assign('res', $addr);
        $this->assign("arrStatus", AddressModel::$STATUS);
            
        return $this->fetch();
    }

    /**
     * 编辑地址提交保存
     * @adminMenu(
     *     'name'   => '编辑地址提交保存',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑地址提交保存',
     *     'param'  => ''
     * )
     */
    public function editPost() {
        $data = $this->request->param();
        if (isset($data['country']) and !empty($data['country'])) {
            $data['coun_id'] = cmf_get_region_id($data['country'],1);
        } else {
            $this->error('请输入国家');
        }
        if (isset($data['province']) and !empty($data['province'])) {
            $data['pro_id'] = cmf_get_region_id($data['province'],2,$data['coun_id']);
        } else {
            $this->error('请输入省/州');
        }
        if (isset($data['city']) and !empty($data['city'])) {
            $data['city_id'] = cmf_get_region_id($data['city'],3,$data['pro_id']);
        } else {
            $this->error('请输入市/区');
        }
        if (!isset($data['zip']) or empty($data['zip'])) {
            $this->error('请输入邮编');
        }
        if ( is_usa($data['country']) ) {
            $data['name'] = $data['country'].' '.$data['detail'].', '.$data['city'].', '.$data['province'].' '.$data['zip'];
        } else {
            $data['name'] = $data['country'].' '.$data['province'].' '.$data['city'].' '.$data['detail'].'，'.$data['zip'];
        }
        unset($data['country']);
        unset($data['province']);
        unset($data['city']);
        
        if (isset($data['code']) and !empty($data['code'])) {
            $length = mb_strlen((string) $data['code']);
            if ($data['type'] == 1) {
                if ($length!=2) {
                    $this->error('驿站编号需输入2位字母');
                }
            } else {
                if ($length!=3) {
                    $this->error('小站编号需输入3位字母');
                }
            }
        }

        $addressModel = new AddressModel();
        $result = $addressModel->validate(true)->allowField(true)->isUpdate(true)->save($data);
        if ($result === false) {
            $this->error($addressModel->getError());
        }

        $this->success("保存成功！", url("address/index",['type'=>$data['type']]));
    }

    /**
     * 更新状态
     * @adminMenu(
     *     'name'   => '更新状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '更新状态',
     *     'param'  => ''
     * )
     */
    public function upStatus() {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }

        $model = new AddressModel();
        $model->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);

        $this->success('操作成功');

    }

}